<template>
  <div class="login_container">
    <!--    登录盒子-->
    <div class="login_box">
      <!--      头像盒子-->
      <div class="avatar_box">
        <img src="../assets/logo.svg" alt="">
      </div>
      <!--      登录表单-->
      <el-form :model="loginForm" ref="loginFormRef" :rules="rules" class="login_form">
        <!--        用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>
        <!--        密码-->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" type="password" prefix-icon="Lock" />
        </el-form-item>
        <!--        按钮-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import router from '@/router/index.js'
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const loginFormRef = ref()
// 登录表单数据绑定对象
const loginForm = ref({
  username: 'admin',
  password: '123456'
})
// 表单验证规则
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 6, message: '长度应在4-6之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 8, message: '长度应在6-8之间', trigger: 'blur' }
  ]
})
// 重置表单方法
const resetLoginForm = () => {
  loginFormRef.value.resetFields()
}
// 登录方法
const login = () => {
  loginFormRef.value.validate(async valid => {
    if (!valid) return
    const { data: result } = await axios.post('/login', loginForm.value)

    if (result.meta.status == 200) {
      ElMessage({
        message: '登录成功！',
        type: 'success',
      })
      console.log(result)
      // 将token到sessionStorage中，因为 token应该在网站关闭时候失效，所以保存在sessionStorage中
      window.sessionStorage.setItem('token', result.data.token)
      // 路由跳转
      router.push('/home')
    }
    else {
      ElMessage.error('登录失败！')
    }
  })
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: pink;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 1px solid #eee;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, .3);

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #ccc;
    }
  }


}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  /* 确定盒模型的宽度和高度包括padding和border，但不包括margin */
  box-sizing: border-box;
}


.btns {
  float: right;
}
</style>